package com.gxa.controller.HospitalController;


import com.gxa.entity.Campus;
import com.gxa.entity.Department;
import com.gxa.service.DepartmentService;
import com.gxa.service.DoctorService;
import com.gxa.utils.R;
import com.gxa.vo.DepartmentVo;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Api(tags="科室管理模块相关接口")
@RestController
public class DepartmentController {


    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private DoctorService doctorService;



    @ApiOperation(value="无参查询接口",notes="查询院区列表",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("depa:sel")
    @PostMapping("/doc/PreAddDepartmentsAll")
    public R PreAddDepartmentsAll(){
        List<DepartmentVo> departmentVos = this.departmentService.queryAll();
        return R.ok().put("data",departmentVos);

    }

    @ApiOperation(value="查询接口",notes="方法的详细描述",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("depa:sel")
    @PostMapping("/doc/QueryDepartments")
    public R QueryDepartments(@ApiParam(name="科室名",value = "根据科室名查询",required = true)@RequestBody DepartmentVo departmentVo){

        List<DepartmentVo> departmentVos = this.departmentService.queryAll1(departmentVo.getName());
        System.out.println(departmentVos);
        return R.ok().put("data",departmentVos);

    }
    @ApiOperation(value="修改前查询接口",notes="方法的详细描述",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("depa:sel")
    @PostMapping("/doc/PreEditDepartments")
    public R PreEditDepartments(@ApiParam(name="科室名",value = "根据科室名查询",required = true)@RequestBody DepartmentVo departmentVo){

        List<DepartmentVo> departmentVos = this.departmentService.queryAll2(departmentVo);


        System.out.println(departmentVos);
        return R.ok().put("data",departmentVos);

    }

    @ApiOperation(value="修改接口",notes="方法的详细描述",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("depa:update")
    @PutMapping("/doc/EditDepartments")
    public R EditDepartments(@ApiParam(name="修改后的数据",value = "根据修改后的数据修改",required = true)@RequestBody DepartmentVo departmentVo){
        Department department = new Department();
        department.setName(departmentVo.getName());
        department.setAddress(departmentVo.getAddress());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        department.setTime(sdf.format(new Date()));
        department.setStatus(departmentVo.getStatus());
        department.setId(departmentVo.getId());
        int id = this.departmentService.queryCampusId(departmentVo.getCampus());
        department.setCampusId(id);

        this.departmentService.updateDepartment(department);


        System.out.println(department);
        System.out.println(departmentVo);
        return R.ok();

    }


    @ApiOperation(value="添加前查询接口",notes="查询院区列表",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("depa:sel")
    @PostMapping("/doc/PreAddDepartments")
    public R PreAddDepartments(){

        List<Campus> campuses = this.departmentService.queryCampus();
        return R.ok().put("data",campuses);

    }



    @ApiOperation(value="添加接口",notes="根据科室参数添加",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("depa:add")
    @PostMapping("/doc/addDepartments")
    public R addDepartments(@ApiParam(name="科室",value = "科室参数",required = true)@RequestBody DepartmentVo departmentVo){

        int id = this.departmentService.queryCampusId(departmentVo.getCampus());
        Department department = new Department();
        department.setAddress(departmentVo.getAddress());
        department.setName(departmentVo.getName());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        department.setTime(sdf.format(new Date()));
        department.setStatus(departmentVo.getStatus());
        department.setCampusId(id);
        department.setSubTypeId(1);
        this.departmentService.insertDepartment(department);

        System.out.println(departmentVo);
        return R.ok();

    }

    @ApiOperation(value="删除接口",notes="根据科室参数删除",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("depa:del")
    @DeleteMapping("/doc/DelDepartments")
    public R DelDepartments(@ApiParam(name="科室",value = "根据科室参数删除",required = true)@RequestBody DepartmentVo departmentVo){
        int id = this.departmentService.queryId(departmentVo);
        departmentVo.setId(id);
        List<Integer> list = this.doctorService.queryDocIdByDepId(id);
        for (int i = 0; i < list.size(); i++) {
            this.doctorService.DelDoctor(list.get(i));
            this.doctorService.DeleteDoctorLabel(list.get(i));
        }
        this.departmentService.deleteById(departmentVo);

        System.out.println(departmentVo);
        return R.ok();

    }
}
